package com.zuihai.app.mvp.ui.activity.detail;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.BallPulseView;
import com.zuihai.app.R;
import com.zuihai.app.mvp.constant.DetailConstant;
import com.zuihai.app.mvp.constant.UniversalConstant;
import com.zuihai.app.mvp.model.entity.Detail;
import com.zuihai.app.mvp.ui.utils.AccountHelper;
import com.zuihai.app.mvp.ui.utils.MyUtils;
import com.zuihai.app.mvp.ui.widget.MyToast;
import com.makeramen.roundedimageview.RoundedImageView;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator n 2017/5/10 0010.
 * 今日头条详情
 */

public class DetailHeadlineFragment extends DetailFragment {
    @BindView(R.id.civ_headimage)
    RoundedImageView mAvatarView1;

    @BindView(R.id.iv_focus)
    ImageView mCareView1;

    @BindView(R.id.tv_name)
    TextView mAuthorView1;
    @BindView(R.id.tv_introduction)
    TextView mIntroduction;

    @BindView(R.id.tv_title)
    TextView mTitleView;

    @BindView(R.id.tv_reward1)
    TextView mRewardView1;

    @BindView(R.id.tv_reward_content)
    TextView mRewardContent;

    @BindView(R.id.tv_reward2)
    TextView mRewardView2;

    @BindView(R.id.tv_focus)
    TextView mFavourView1;
    @BindView(R.id.tv_focus_content)
    TextView mFavourViewContent;

    @BindView(R.id.iv_praise)
    ImageView mFavour;

    @BindView(R.id.tv_favour2)
    TextView mFavourView2;
    @BindView(R.id.tv_comment_count)
    TextView mCommentCountView;

    @BindView(R.id.iv_collect)
    TextView mCollectView;
    @BindView(R.id.ll_dianzan)
    LinearLayout llDianzan;
    private String mHeadlineType;
    private String mDetailId;
    private boolean isDraft;
    @BindView(R.id.footer_layout)
    LinearLayout footLayout;
    @BindView(R.id.ll_more)
    LinearLayout llMore;
    @BindView(R.id.iv_vip)
    ImageView ivVip;

    private  String huifuID=null;

    public static DetailHeadlineFragment newInstance(String detailId) {
        TwinklingRefreshLayout.setDefaultFooter(BallPulseView.class.getName());
        Bundle args = new Bundle();
        args.putString(DetailConstant.EXTRA_ID, detailId);
        DetailHeadlineFragment fragment = new DetailHeadlineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static DetailHeadlineFragment newInstance(String detailId, int headlineType) {
        TwinklingRefreshLayout.setDefaultFooter(BallPulseView.class.getName());
        Bundle args = new Bundle();
        args.putString(DetailConstant.EXTRA_ID, detailId);
        args.putInt(DetailConstant.EXTRA_HEADLINE_TYPE, headlineType);
        DetailHeadlineFragment fragment = new DetailHeadlineFragment();
        fragment.setArguments(args);
        return fragment;
    }
    public static DetailHeadlineFragment newInstance(String detailId, String headlineType, String  huifuId,Boolean isDraft) {
        TwinklingRefreshLayout.setDefaultFooter(BallPulseView.class.getName());
        Bundle args = new Bundle();
        args.putBoolean(DetailConstant.EXTRA_IS_DRAFT, isDraft);
        args.putString(DetailConstant.EXTRA_ID, detailId);
        args.putString(DetailConstant.EXTRA_HEADLINE_TYPE, headlineType);
        args.putString(DetailConstant.EXTRA_HUIFU_ID, huifuId);
        DetailHeadlineFragment fragment = new DetailHeadlineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int initContentView() {
        return R.layout.fragment_detail_article;
    }



    /**
     * 收藏
     *
     * @param view
     */
    @OnClick(R.id.iv_collect)
    public void onCollect(View view) {
        if (AccountHelper.getInstance().isLogin()) {
            mCollectView.setSelected(!mCollectView.isSelected());
            mPresenter.headlineCollect(mDetailId, mDetail.type, mCollectView.isSelected(), mDetail.authorId);
        } else {

        }
    }

    @BindView(R.id.cb_dianzan)
    CheckBox cbDianzan;
    /**
     * 文章点赞
     *
     * @param view
     */
    @OnClick({R.id.iv_favour2, R.id.iv_praise,R.id.cb_dianzan})
    public void onFavour(View view) {
        if (AccountHelper.getInstance().isLogin()) {
            if (!mDetail.isFavour) {
                mFavour.setSelected(!mFavour.isSelected());
                mPresenter.handlineFavour(mDetail.id, mDetail.authorId,mDetail.type);
                mDetail.isFavour = true;
                mDetail.favourCount++;
                String formatFavour = String.format("%s", mDetail.favourCount);
                mFavourView1.setText(formatFavour);
                mFavourView2.setText(formatFavour);
                mFavourViewContent.setText("人点赞");
                // mPresenter.handlineFavour(mDetail.id, mDetail.authorId);
                mFavourHint.setVisibility(View.GONE);
                cbDianzan.setSelected(true);
            } else {
                MyToast.showError("已经点过赞了");
            }
        } else {

        }
    }

    /**
     * 推荐
     *
     * @param view
     */
    @OnClick({R.id.recommend_layout1, R.id.recommend_layout2, R.id.iv_recommend})
    public void onRecomment(View view) {
        if (AccountHelper.getInstance().isLogin()) {
            if (mDetail != null) {
                if (AccountHelper.getInstance().isLogin()) {
                    //使用新的推荐 跳转

                   // start(DetailRecommendFragment.newInstance(mDetail));
                } else {

                }
            }
        } else {

        }
    }








    @Override
    protected void shareLuckyMoney() {
        mPresenter.homeShareLuckyMoney(mDetailId);
    }

    @Override
    protected void initData() {
        super.initData();
        isDraft=getArguments().getBoolean(DetailConstant.EXTRA_IS_DRAFT);
        if (isDraft){
            footLayout.setVisibility(View.GONE);
            llDianzan.setVisibility(View.GONE);
            llMore.setVisibility(View.GONE);
            setupActionThree(0, new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }

    @Override
    public void fillContent(Detail detail) {
        super.fillContent(detail);
        if (detail.isRenzheng){
            ivVip.setVisibility(View.VISIBLE);
        }else {
            ivVip.setVisibility(View.GONE);
        }
        if (!"已发布".equals(detail.shenhe)){
            footLayout.setVisibility(View.GONE);
            llDianzan.setVisibility(View.GONE);
            llMore.setVisibility(View.GONE);
            setupActionThree(0, new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
        String formatFavour = String.format(mFavourView2.getText().toString(), detail.favourCount);
        if (detail.favourCount>0){
            mFavourView1.setText(formatFavour);
            mFavourView2.setText(formatFavour);
            mFavourViewContent.setText("人点赞");
        }else {
            mFavourView1.setText("");
            mFavourViewContent.setText("点赞");
        }

        if (Integer.parseInt(detail.rewardCount)>0){
            String rewardFavour = String.format(mRewardView1.getText().toString(), detail.rewardCount);
            mRewardView1.setText(rewardFavour);
            mRewardView2.setText(rewardFavour);
            mRewardContent.setText("人敬酒");
        }else {
            mRewardView1.setText("");
            mRewardContent.setText("敬酒");
        }
        String mTitle=detail.title.trim();
        mTitleView.setText(mTitle);
        mFavour.setSelected(detail.isFavour);
        //作者
        mAuthorView1.setText(detail.authorName);
        //签名
        if (detail.authorIntroduction.length() > 0) {
            mIntroduction.setText(detail.authorIntroduction);
            mIntroduction.setVisibility(View.VISIBLE);
        } else {
            mIntroduction.setVisibility(View.GONE);
            mIntroduction.setText(getResources().getString(R.string.authorIntroduction));
        }
        //用户头像
        MyUtils.headImageGlide(getActivity(),detail.authorAvatar,mAvatarView1);
        //评论量
        int commentCount = detail.commentCount;
        refreshCommentCount(commentCount);
        //关注
        care(detail);
        //收藏
        boolean isCollect = detail.isCollect;
        mCollectView.setSelected(isCollect);
        cbDianzan.setSelected(detail.isFavour);
    }



    private void care(Detail detail) {
        String uid = AccountHelper.getInstance().getUid();
        if (uid.equals(detail.authorId)) {
            mCareView1.setVisibility(View.INVISIBLE);
        }
        boolean isCare = detail.isCare;
        mCareView1.setSelected(isCare);
        String sex = detail.releaseSex;
        if (sex.equals(UniversalConstant.MAN)) {
            mCareView1.setImageResource(R.drawable.sex_man_selector);
        } else {
            mCareView1.setImageResource(R.drawable.sex_woman_selector);
        }
        if (isCare) {
            mCareView1.setSelected(true);
        } else {
            mCareView1.setSelected(false);
        }
    }

    @Override
    public void refreshCommentCount(int count) {
        mCommentCountView.setVisibility(count > 0 ? View.VISIBLE : View.GONE);
        mCommentCountView.setText(count + "");
    }

    @Override
    protected void requestData() {
        mHeadlineType = getArguments().getString(DetailConstant.EXTRA_HEADLINE_TYPE);
        mDetailId = getArguments().getString(DetailConstant.EXTRA_ID);
        //2018.8.29 注释掉，用于替换回复的逻辑
        /*PinglunContent mComment = (PinglunContent) getArguments().getSerializable(DetailConstant.EXTRA_COMMENT_TYPE);
        if (mComment!=null){
            start(DetailCommentInfoFragment.newInstance(mComment));
        }*/
        huifuID=getArguments().getString(DetailConstant.EXTRA_HUIFU_ID);
        mPresenter.requestHeadline(mDetailId);

        if (huifuID!=null){
            LogUtils.e("此时HuifuId :"+huifuID);

        }else {
            LogUtils.e("此时HuifuId  null:"+huifuID);
        }
    }

    public void initNetData(){
        mPresenter.requestHeadlineIntentTuijian(mDetailId);
        /*switch (mHeadlineType) {
            case ReleaseConstant.RELEASE_HEADLINE:
                mPresenter.requestHeadline(mDetailId);
                break;
            case ReleaseConstant.RELEASE_HOT:
                mPresenter.requestHot(mDetailId);
                break;
            case ReleaseConstant.RELEASE_PERSION:
                mPresenter.requesChara(mDetailId);
                break;
            case ReleaseConstant.RELEASE_STORY:
                mPresenter.requestStory(mDetailId);
                break;
            case ReleaseConstant.zhanwei:
                mPresenter.requestSecret(mDetailId);
                break;
            case ReleaseConstant.RELEASE_HELP:
                mPresenter.requestHelp(mDetailId);
                break;
            case ReleaseConstant.RELEASE_SHOPPING:
                mPresenter.requestShopping(mDetailId);
                break;
            case ReleaseConstant.RELEASE_PERYPHERY:
                mPresenter.requestPeryphery(mDetailId);
                break;
            case ReleaseConstant.RELEASE_ACTIVITY:
                mPresenter.requestActivity(mDetailId);
                break;
        }*/
    }

    @Override
    public void intentTuijian() {
        LogUtils.e("intentTuijian");

       // start(DetailRecommendFragment.newInstance(mDetail));

    }

    @org.simple.eventbus.Subscriber(tag = "request_detail")
    public void refreshData(String msg) {

        LogUtils.e("request_detail");
        initNetData();


    }

    @Override
    protected void requestComment() {
        mPresenter.requestComment(mDetail.id,isFirstComment);

    }



    @Override
    protected void onRecommetClick(BaseQuickAdapter adapter, View view, int position) {
        Detail.Recommend recomment = (Detail.Recommend) adapter.getItem(position);
        String id = recomment.id;
        int type=Integer.parseInt(recomment.type);
        start(DetailHeadlineFragment.newInstance(id,type));
    }

    @Subscriber(tag = "rewardnumber")
    public void refresh(int rewardNumber){
        mRewardView1.setText(rewardNumber);
        mRewardView2.setText(rewardNumber);
        mRewardContent.setText("人敬酒");
    }

}
